import {
  getToken
} from "@/utils/auth";
import asyncRoutes from "./asyncRoutes";
import staticRoutes from "./staticRoutes";
import store from "@/store";

function isWhiteName(path) {
  const whiteName = ["/login", "/404"];
  return whiteName.includes(path);
}

export default async function (to, from, next) {
  const token = getToken();
  const isWhite = isWhiteName(to.path);
  //判断没有token直接跳转登录页
  if (!isWhite && !token) return next("/login");
  //动态获取路由权限
  if (!store.state.user.account && !isWhite) {
    await store.dispatch("getUserInfo");
    const menuList = store.getters.menu.map(_ => _.viewName);
    const result = [...asyncRoutes, ...staticRoutes].filter(_ =>
      menuList.includes(_.name)
    );
    store.commit("saveRoutes", result);
  }
  next();
}